<template>
    <div>
        <Spin fix v-if="isSpin">
            <Icon type="ios-loading" size=18 class="demo-spin-icon-load"></Icon>
            <div>正在生成企业安全总监汇总表，请稍后</div>
        </Spin>
        <Form :label-width="100" ref="formInline" :model="formInline">
            <Row :gutter="16" class="query-form2" >
                <i-col span="8">
                    <FormItem label="单位名称：">
                        <Input v-model="formInline.orgName" placeholder="请输入公司名称"/>
                    </FormItem>
                </i-col>
                <i-col span="8">
                    <FormItem label="总监姓名：">
                        <Input v-model="formInline.userName" placeholder="请输入总监姓名"/>
                    </FormItem>
                </i-col>
                <i-col span="8">
                    <div style="float: right;">
                        <Button class="ivu-btn-query"  icon="md-search"  @click="query">查询</Button>
                        <Button class="ivu-btn-reset" style="margin-left: 10px" @click="cancel">重置</Button>
                    </div>
                </i-col>
            </Row>
        </Form>
        <Row type="flex" justify="start" class="query-form">
            <i-col>
                <Button icon="md-cloud-download" class="ivu-btn-add" @click="exportExcel">导出</Button>
            </i-col>
        </Row>
        <Table ref='table' style="margin-top: 10px" :loading="loading" border :columns="columns" no-data-text="没有查询到相关数据！"
               :data="infos" width="100%" :height="tableHeight" highlight-row stripe size="small"></Table>
        <Row type="flex" justify="end" >
            <i-col>
                <Page :total="pagedata.total" :current="pagedata.currentPage" :page-size="pagedata.pageSize"
                      @on-page-size-change="pageSizeChange" @on-change="pageChange" :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
            </i-col>
        </Row>

    </div>
</template>

<script>

    import {Api} from '../../../api'
    import {sessionStorage} from '@/common/cacheUtils.js';

    export default {
        name: "securityDirectorSummary",
        data(){
            return {
                loading: false,
                isSpin: false,
                formInline: {},
                columns: [
                    {
                        type: 'index',
                        title: '序号',
                        width: 80,
                        align: 'center',
                        fixed: 'left',
                    },
                    {
                        title: '二级集团',
                        align: 'center',
                        key: 'twoLevelOrgName',
                        minWidth: 200,
                        tooltip: true,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.twoLevelOrgName || '--')
                            ]);
                        }
                    },
                    {
                        title: '企业名称',
                        align: 'center',
                        key: 'orgName',
                        minWidth: 200,
                        tooltip: true,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.orgName || '--')
                            ]);
                        }
                    },
                    {
                        title: '姓名',
                        align: 'center',
                        key: 'userName',
                        minWidth: 120,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.userName || '--')
                            ]);
                        }
                    },
                    {
                        title: '年龄',
                        align: 'center',
                        key: 'age',
                        minWidth: 80,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.age || '--')
                            ]);
                        }
                    },
                    {
                        title: '性别',
                        align: 'center',
                        key: 'gender',
                        minWidth: 80,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.gender || '--')
                            ]);
                        }
                    },
                    {
                        title: '政治面貌',
                        align: 'center',
                        key: 'politicalAffiliationName',
                        minWidth: 120,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.politicalAffiliationName || '--')
                            ]);
                        }
                    },
                    {
                        title: '出生年月',
                        align: 'center',
                        key: 'birthday',
                        minWidth: 140,
                        render:(h,params)=>{
                            return h('div',[
                                h('span', params.row.securityDirectorInfo.birthday!= null ?this.formatDate(new Date(params.row.securityDirectorInfo.birthday), 'yyyy-MM-dd'): '--')
                            ]);
                        }
                    },
                    {
                        title: '手机号码',
                        align: 'center',
                        key: 'phoneNumber',
                        minWidth: 120,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.phoneNumber || '--')
                            ]);
                        }
                    },
                    {
                        title: '学历',
                        align: 'center',
                        key: 'educationName',
                        minWidth: 120,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.educationName || '--')
                            ]);
                        }
                    },
                    {
                        title: '专业',
                        align: 'center',
                        key: 'major',
                        minWidth: 150,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.major || '--')
                            ]);
                        }
                    },
                    {
                        title: '现职务（岗位）',
                        align: 'center',
                        key: 'currentPosition',
                        minWidth: 150,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.currentPosition || '--')
                            ]);
                        }
                    },
                    {
                        title: '工作年限',
                        align: 'center',
                        key: 'workYear',
                        minWidth: 120,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.workYear || '--')
                            ]);
                        }
                    },
                    {
                        title: '现岗位工作年限',
                        align: 'center',
                        key: 'currentPositionWorkYear',
                        minWidth: 150,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.currentPositionWorkYear || '--')
                            ]);
                        }
                    },
                    {
                        title: '安全工作年限',
                        align: 'center',
                        key: 'relevantWorkYear',
                        minWidth: 120,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.relevantWorkYear || '--')
                            ]);
                        }
                    },
                    {
                        minWidth: 180,
                        title: '任职资格',
                        align: 'center',
                        key: 'qualification',
                        tooltip: true,
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.qualification || '--')
                            ]);
                        }
                    },
                    {
                        minWidth: 180,
                        title: '初审意见',
                        align: 'center',
                        key: 'groupOpinion',
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.securityDirectorInfo.groupOpinion || '--')
                            ]);
                        }
                    },
                    {
                        minWidth: 120,
                        title: '原总监姓名',
                        align: 'center',
                        key: 'oldUserName',
                        render:(h,params)=>{
                            return h('div',[
                                h('span',params.row.oldUserName || '--')
                            ]);
                        }
                    },
                ],
                infos: [],
                tableHeight: '',
                pagedata: {
                    total: 0,
                    pageSize: 10,
                    currentPage: 1
                },
                pageSizeArray: [10, 20, 50],
                userInfo: sessionStorage('userId').getObject('userId'),
            }
        },
        mounted(){
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;
            this.query();
        },
        methods: {
            query(){
                this.loading = true;
                let data = {
                    startPage: this.pagedata.currentPage,
                    pageSize: this.pagedata.pageSize,
                    orgId: this.userInfo.companyId
                };
                data = Object.assign(data,JSON.parse(JSON.stringify(this.formInline)));
                this.$http.get(Api.securityDirectorInfo + '/summary', {
                    params: data
                }).then(function(res){
                    this.infos = res.data.data.data;
                    this.pagedata.total = res.data.data.totalSize;
                    this.loading = false;
                }, function(res){
                    this.$Message.error('请求失败');
                });

            },
            cancel(){
                this.formInline = {};
                this.query();
            },
            pageSizeChange(pageSize){
                this.pagedata.pageSize = pageSize;
                this.query();
            },
            pageChange(pageNum){
                this.pagedata.currentPage = pageNum;
                this.query();
            },
            exportExcel(){
                let data = {orgId: this.userInfo.companyId };
                console.log(data);
                data = Object.assign(data,JSON.parse(JSON.stringify(this.formInline)));
                this.isSpin = true;
                this.$http.get(Api.securityDirectorInfo + '/summary/excel',{
                    params: data,
                    responseType: 'arraybuffer'
                }).then((res) => {
                    const aLink = document.createElement("a");
                    let blob = new Blob([res.data], {
                        type: "application/vnd.ms-excel"
                    })
                    aLink.href = URL.createObjectURL(blob);
                    aLink.download = "安全总监汇总("+ this.formatDate(new Date(), "yyyy-MM-dd")+ ").xlsx" //标题
                    aLink.click();
                    document.body.appendChild(aLink);
                    this.isSpin = false;
                }, function(res){
                    this.$Message.error('生成失败');
                    this.isSpin = false;
                })
            },
            formatDate(date, fmt) {
                if (typeof date == 'string') {
                    return date;
                }
                if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";
                if (!date || date == null) return null;
                let o = {
                    'M+': date.getMonth() + 1, // 月份
                    'd+': date.getDate(), // 日
                    'h+': date.getHours(), // 小时
                    'm+': date.getMinutes(), // 分
                    's+': date.getSeconds(), // 秒
                    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
                    'S': date.getMilliseconds() // 毫秒
                }
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
                for (let k in o) {
                    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
                }
                return fmt
            },
        }
    }
</script>

<style scoped>
    .demo-spin-icon-load{
        animation: ani-demo-spin 1s linear infinite;
    }
</style>
